package com.guodaxia.gzh.tools;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


//参考
//https://blog.csdn.net/qq_42046421/article/details/132108654?ops_request_misc=%257B%2522request%255Fid%2522%253A%252205237280c77c978fb83905905fc30887%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=05237280c77c978fb83905905fc30887&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-18-132108654-null-null.142^v102^pc_search_result_base4&utm_term=esayexcel%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA&spm=1018.2226.3001.4187
//excel 监听器
public class ExcelListener extends AnalysisEventListener<EmployeeBo> {

    // 批次大小   每次读取一万，可调整
    private static final int BATCH_SIZE = 10000;

    //批次数据
    private List<EmployeeBo> dataList = new ArrayList<>();

    @Override
    public void invoke(EmployeeBo data, AnalysisContext context) {
        // 将数据加工处理添加到 dataList 中。加工略
        dataList.add(data);
        // 当读取到 BATCH_SIZE 指定的行数时，处理数据，并清空列表
        if (dataList.size() >= BATCH_SIZE) {
            processBatchData(dataList);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 确保最后的数据也被处理
        if (!dataList.isEmpty()) {
            processBatchData(dataList);
            dataList.clear();
        }
    }

    @Override
    public void invokeHeadMap(Map headMap, AnalysisContext context) {
        // 处理表头信息
    }

    public void processBatchData(List<EmployeeBo> dataList) {
        // 批量插入逻辑
        //略
/*        for (EmployeeBo employeeBo : dataList) {
            System.out.println(employeeBo);
        }*/
        //清空
        dataList.clear();
    }
}
